/*
 * one light at every node, lighting direction determined by node's (i, j)
 * use i and j with % 2
 * LIGHT FACING DIRECTIONS
 * 0, 0 - up / right
 * 1, 0 - up / left
 * 0, 1 - down / right
 * 1, 1 = down / left
 */
package traffic;

public class Lights
{
    private int row, col, greenState;
    
    Lights(int i, int j)
    {
        createLight(i, j);
    }
    
    private void createLight(int i, int j)
    {
        row = i;
        col = j;
        greenState = 0;
    }
    
    public void changeLightState()
    {
        greenState = (greenState + 1)%2; //either 0 or 1, lights only bi-directional
        /*
         * This is now paired with a drawing function that will update the light
         * image depending on the value of greenState
         */
        
    }
    
    public int getLightDirection()
    {
        /* This is not taking into account that the intersections along the edges of our grid
         * may only give the car one option of which direction to go.
         * But since the car already has it's path, and only goes when the light
         * is green in the direction of the car, it doesn't matter.
         * May need to re-visit this when implementing the images.
         * UPDATE: Only need to change the images, this function is fine.
         * 
         * 0, 0 - up / right
         * 1, 0 - up / left
         * 0, 1 - down / right
         * 1, 1 = down / left
         * 
         * return value meaning:
         * 0 - up
         * 1 - right
         * 2 - down
         * 3 - left
         */
        if(row%2 == 0 && col%2 == 0)
        {
            if(greenState == 0)
                return 0; //up
            else if(greenState == 1)
                return 1; //right
        }
        else if(row%2 == 1 && col%2 == 0)
        {
            if(greenState == 0)
                return 0; //up
            else if(greenState == 1)
                return 3; //left
        }
        else if(row%2 == 0 && col%2 == 1)
        {
            if(greenState == 0)
                return 2; //down
            else if(greenState == 1)
                return 1; //right
        }
        else if(row%2 == 1 && col%2 == 1)
        {
            if(greenState == 0)
                return 2; //down
            else if(greenState == 1)
                return 3; //left
        }
        //if all of the cases above fail
        
        System.err.println("There was an error in getLightDirection.");
        System.exit(1);
        return -1;
        
        
    }
    
    public int getLightState()
    {
        return greenState;
    }
    
    public void setLightState(int i)
    {
        if(i == 0 || i == 1)
            greenState = i;
        else
            System.err.println("Illegal value for setLightState.");
    }
    
    
    
    /*
    public void getLightStates() //strictly for testing
    {
        int i, j, buffer;
        
        for(i = 0; i<10; i++)
        {
            for(j = 0; j<10; j++)
            {
                buffer = lightMatrix[i][j].getLightDirection();
                
                if(buffer == 0)
                    System.out.println("The green light at (" + i + ", " + j + ") is directed Up");
                else if(buffer == 1)
                    System.out.println("The green light at (" + i + ", " + j + ") is directed Right");
                else if(buffer == 2)
                    System.out.println("The green light at (" + i + ", " + j + ") is directed Down");
                else if(buffer == 3)
                    System.out.println("The green light at (" + i + ", " + j + ") is directed Left");
                else 
                    System.out.println("Something is wrong.");
            }
        }
    }
    * 
    */
    
    
}
